package com.noble.meether.ui.wallet.detail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.noble.meether.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.module.util.PickerDialogUtil;
import com.noble.glamour.module.util.TimeUtil;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.view.custom.state.CustomStatusListView;
import com.noble.library.widget.ListView.xlistview.ExListView;
import com.noble.meether.ui.wallet.detail.bean.Income;
import com.noble.meether.ui.wallet.detail.bean.IncomeList;

import java.util.ArrayList;
import java.util.List;

/**
 * 收益明细
 */
public class WalletDetailAct extends BaseAppCompatActivity implements RequestComplete, ExListView.IXListViewListener, View.OnClickListener {
    private CustomStatusListView customStatusListView;
    private ExListView exListView;
    private TextView tv_month;

    private WalletDetailAdapter adapter;
    private List<Income> data = new ArrayList<>();

    private int page = 1;
    private String month = "";      // 查询月份

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_wallet_detail);
        initView();
        onRefresh();
    }

    private void initView() {
        setBackView("收益明细");
        tv_month = findViewById(R.id.tv_month);
        findViewById(R.id.btn_month).setOnClickListener(this);
        customStatusListView = findViewById(R.id.myfriend_list);
        exListView = customStatusListView.getExListView();
        exListView.setXListViewListener(this);
        exListView.setPullRefreshEnable(false);
        exListView.setPullLoadEnable(true);
        adapter = new WalletDetailAdapter(this, data);
        exListView.setAdapter(adapter);
        customStatusListView.showLoading();
    }

    @Override
    public void onRefresh() {
        exListView.setPullLoadEnable(true);
        page = 1;
        ModuleMgr.getCommonMgr().reqWalletDetail(page, month, this);
    }

    @Override
    public void onLoadMore() {
        page++;
        ModuleMgr.getCommonMgr().reqWalletDetail(page, month, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.isOk()) {
            IncomeList infoList = (IncomeList) response.getBaseData();

            if (infoList.getIncomeList().size() != 0) {
                if (page == 1) {
                    if (data.size() != 0) {
                        data.clear();
                    }
                }
                data.addAll(infoList.getIncomeList());
                if (data.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                customStatusListView.showExListView();
            } else {
                if (page == 1) {
                    customStatusListView.showNoData();
                } else {
                    exListView.setPullLoadEnable(false);
                }
            }

            if (page == 1) {
                exListView.stopRefresh();
            } else {
                exListView.stopLoadMore();
            }
        } else {
            customStatusListView.showNoData();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_month:   // 月份选择
                PickerDialogUtil.showIOSTimePicker(this, (date, v) -> {
                    month = TimeUtil.getFormatData(date, "yyyyMM");
                    tv_month.setText(TimeUtil.getFormatData(date, "yyyy年MM月"));
                    onRefresh();
                });
                break;
        }
    }
}